define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'services/index',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'avatar', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'username', title: __('前端账号'), operate: 'LIKE' },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        {
                            field: 'is_typs',
                            title: '客服类型',
                            sortable: true,
                            formatter: function(value, row, index) {
                                if (row.is_typs == 1) {
                                    return '<span style="color:#336699;">' + value + '</span>'
                                } else if (row.is_typs == 2) {
                                    return '<span style="color:#008800;">' + value + '</span>';
                                } else if (row.is_typs == 3) {
                                    return '<span style="color:#ff3300;">业务员</span>';
                                } else if (row.is_typs == 4) {
                                    return '<span style="color:#6699FF;">业务主管</span>';
                                } else if (row.is_typs == 5) {
                                    return '<span style="color:#666600;">客服</span>';
                                } else {
                                    return '<span style="color:#669966;">未知</span>';
                                }

                            }
                        },
                        { field: 'depart', title: __('所属部门'), operate: 'LIKE' },
                        { field: 'admins', title: __('后端账号'), operate: 'LIKE' },
                        { field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true },
                        { field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true },
                        { field: 'logintime', title: __('登录时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        { field: 'loginip', title: __('登录IP'), formatter: Table.api.formatter.search },
                        { field: 'jointime', title: __('加入时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        { field: 'joinip', title: __('加入IP'), formatter: Table.api.formatter.search },
                        { field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function() {
            Controller.api.bindevent();
        },
        edit: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});